package com.tom.service.impl;

import com.tom.constant.ArticleChangeEnum;
import com.tom.constant.CacheEnum;
import com.tom.dto.BaseArticleDTO;
import com.tom.dto.CMSQueryDTO;
import com.tom.service.ArticleRemoteService;
import com.tom.service.CMSArticleApi;
import com.tom.service.SynchronousArticleService;
import com.tom.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @Author 徐望成
 * @Date 2023/12/18 15:36
 * @PackageName:com.tom.service.impl
 * @ClassName: SynchronousArticleServiceImpl
 * @Description: SynchronousArticleServiceImpl
 * @Copyright: 2019 www.tomonline-inc.com Inc. All rights reserved.
 * 注意：本内容仅限于TOM集团内部传阅，禁止外泄以及用于其他的商业目
 * @Version 1.0
 */
@Slf4j
@Service
public class SynchronousArticleServiceImpl implements SynchronousArticleService {


    @Autowired
    private ArticleRemoteService articleRemoteService;

    @Autowired
    private CMSArticleApi cmsArticleApi;

    @Value("${bw-article.SiteNo}")
    private String siteNo;

    @Override
    public void addArticle() {
        // 1. 构建查询参数
        CMSQueryDTO queryDTO = new CMSQueryDTO();
        Calendar instance = Calendar.getInstance();
        queryDTO.setEndTime(DateUtil.timeConvertToString(instance.getTime()));
        instance.add(Calendar.HOUR, -24);
        queryDTO.setStartTime(DateUtil.timeConvertToString(instance.getTime()));
        queryDTO.setCache(CacheEnum.NO.getCache());
        queryDTO.setModType(ArticleChangeEnum.INSERT.getType());

        // 2. 获取最近30分钟新增文章
        List<BaseArticleDTO> articleList = cmsArticleApi.getArticleList(queryDTO);

        // 3. 遍历文章
        for (BaseArticleDTO article : articleList) {
            articleRemoteService.getArticleContent(String.valueOf(article.getDataTab()), article.getId(), siteNo, "2");
        }
    }

    @Override
    public void updateArticle() {
        // 1. 构建查询参数
        CMSQueryDTO queryDTO = new CMSQueryDTO();
        Calendar instance = Calendar.getInstance();
        queryDTO.setEndTime(DateUtil.timeConvertToString(instance.getTime()));
        instance.add(Calendar.DATE, -1);
        queryDTO.setStartTime(DateUtil.timeConvertToString(instance.getTime()));
        queryDTO.setCache(CacheEnum.NO.getCache());
        queryDTO.setModType(ArticleChangeEnum.UPDATE.getType());

        // 2. 获取最近一天修改的文章
        List<BaseArticleDTO> articleList = cmsArticleApi.getArticleList(queryDTO);

        // 3. 遍历文章
        for (BaseArticleDTO article : articleList) {
            articleRemoteService.getArticleContent(String.valueOf(article.getDataTab()), article.getId(), siteNo, "1");
        }
    }
}
